package com.adgox.tiantianbiting.mine.help;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.EditText;
import android.widget.Toast;

import com.adgox.tiantianbiting.R;
import com.adgox.tiantianbiting.bean.BaseResponseBean;
import com.adgox.tiantianbiting.bean.UserInfo;
import com.adgox.tiantianbiting.net.callback.JsonCallback;
import com.adgox.tiantianbiting.utils.ApiConstant;
import com.adgox.tiantianbiting.utils.DialogUtils;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.rae.swift.session.SessionManager;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HelpActivity extends AppCompatActivity {

    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.et_mobile)
    EditText etMobile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_help);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.img_finish)
    public void onFinishClicked() {
        finish();
    }

    @OnClick(R.id.tv_commit)
    public void onCommitClicked() {
        String content = etContent.getText().toString().trim();
        String mobile = etMobile.getText().toString().trim();
        UserInfo user = SessionManager.getDefault().getUser();
        int id = user.getId();

        if (content == null || content.length() == 0) {
            Toast.makeText(this, "反馈内容不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (mobile == null || mobile.length() != 11){
            Toast.makeText(this, "手机号格式不正确", Toast.LENGTH_SHORT).show();
            return;
        }
        DialogUtils.showLoadingDialog(this);
        Gson gson = new Gson();
        HashMap<String, Object> map = new HashMap<>();
        map.put("adviseContent",content);
        map.put("mobile",mobile);
        map.put("userId",id);
        String s = gson.toJson(map);
        OkGo.<BaseResponseBean<String>>post(ApiConstant.BASE_URL+ApiConstant.FEEDBACK)
                .upJson(s)
                .execute(new JsonCallback<BaseResponseBean<String>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<String>> response) {
                        DialogUtils.dismiss();
                        if ("0".equals(response.body().getCode())){
                            //反馈成功
                            onFeedBackSuccess();
                        } else {
                            //反馈失败
                            onFeedBackFailed();
                        }
                    }
                });
    }

    private void onFeedBackFailed() {
        Toast.makeText(this, "操作失败，请检查重试！", Toast.LENGTH_SHORT).show();
    }

    private void onFeedBackSuccess() {
        Toast.makeText(this, "反馈成功，感谢您的支持！", Toast.LENGTH_SHORT).show();
        etContent.setText(null);
        etMobile.setText(null);
    }
}
